<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="24">
            <p-form-item label="标签模板编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                readonly
                v-decorator="[ 'labelTemplateNo', validatorRules.labelTemplateNo]"
                placeholder="请输入标签模板编号"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="标签模板名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'labelTemplateName', validatorRules.labelTemplateName]"
                placeholder="请输入标签模板名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="标签模板地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-select
                v-decorator="[ 'ureportId', validatorRules.ureportId]"
                @change="handleChange"
              >
                <p-select-option
                  :value="item.id"
                  v-for="(item, index) in ureportList"
                  :key="index"
                >{{item.name}}</p-select-option>
              </p-select>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="标签模板尺寸" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['labelTemplateLength', validatorRules.labelTemplateLength]"
                :trigger-change="true"
                dictCode="mes-label-template-length"
                placeholder="请选择"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="标签模板描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'labelTemplateDescrtion', validatorRules.labelTemplateDescrtion]"
                placeholder="请输入标签模板描述"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="标签类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['labelType', validatorRules.labelType]"
                :trigger-change="true"
                dictCode="mes_label_type"
                placeholder="请选择标签类型"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="责任部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-depart-mes
                v-decorator="['dptname', validatorRules.dptname]"
                :trigger-change="true"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="标签纸颜色" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['labelColor', validatorRules.labelColor]"
                :trigger-change="true"
                dictCode="mes_label_color"
                placeholder="请选择标签纸颜色"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="碳带型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['ribbonsType', validatorRules.ribbonsType]"
                :trigger-change="true"
                dictCode="mes-ribbons-type"
                placeholder="请选择"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="打印机型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['printerType', validatorRules.printerType]"
                :trigger-change="true"
                dictCode="mes_printer_type"
                placeholder="请选择打印机型号"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="是否包含流水号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['containSn', validatorRules.containSn]"
                :trigger-change="true"
                dictCode="ba_yn"
                placeholder="请选择是否包含流水号"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-switch
                placeholder="String"
                v-decorator="['status']"
                field="status"
                dictCode="mes_open_close"
                @callback="popupCallback"
              />
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入备注"></p-input>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction, putAction, getAction, api } from "@/api/manage";
import fSwitch from "@/components/switch";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";
import { validateDuplicateValue } from "@/utils/util";
import JSelectDepartMes from "@/components/jeecgbiz/JSelectDepartMes";
import JDictSelectTag from "@/components/dict/JDictSelectTag";
import { ajaxGetDictItems } from "@/api/api";

export default {
  name: "MesBcTemplateModal",
  mixins: [SubmitOnchangMixin],
  components: {
    JSelectDepartMes,
    JDictSelectTag,
    fSwitch,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        labelTemplateNo: {
          rules: [{ required: true, message: "请输入标签模板编号!" }],
        },
        labelTemplateName: {
          rules: [{ required: true, message: "请输入标签模板名称!" }],
        },
        ureportId: {
          rules: [{ required: true, message: "请选择标签模板地址!" }],
        },
        labelTemplateLength: {
          rules: [{ required: true, message: "请输入标签模板尺寸!" }],
        },
        labelTemplateDescrtion: { rules: [] },
        labelType: { rules: [{ required: true, message: "请输入标签类型!" }] },
        dptname: { rules: [{ required: true, message: "请输入责任部门!" }] },
        labelColor: {
          rules: [{ required: true, message: "请输入标签纸颜色!" }],
        },
        ribbonsType: {
          rules: [{ required: true, message: "请选择碳带型号!" }],
        },
        printerType: {
          rules: [{ required: true, message: "请输入打印机型号!" }],
        },
        containSn: {
          rules: [{ required: true, message: "请输入是否包含流水号!" }],
        },
        status: { rules: [{ required: true, message: "请输入状态!" }] },
        remark: { rules: [] },
      },
      ureportList: [],
      url: {
        add: "/mes-code/mesBcTemplate",
        edit: "/mes-code/mesBcTemplate",
        ureportList: "/mes-code/uReport/all",
      },
    };
  },
  created() {
    this.getTemplateUrlList();
  },
  methods: {
    getTemplateUrlList() {
      getAction(this.url.ureportList).then((res) => {
        if (res.success) {
          this.ureportList = res.data;
        }
      });
    },

    add() {
      this.edit({});
      this.getRuleCodeValue();
    },
    getRuleCodeValue() {
      let param = {
        commonFormData: this.model,
        rules: [
          { ruleCode: "mes_label_template_no", columnName: "labelTemplateNo" },
        ],
      };

      putAction(api.ruleBaseURL, param).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          console.log("生成的编码：" + res.data);
          const item = res.data;
          this.form.setFieldsValue(pick(item, Object.keys(item)));
        }
      });
    },

    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "createBy",
            "createTime",
            "updateBy",
            "updateTime",
            "labelTemplateNo",
            "labelTemplateName",
            "labelTemplateLength",
            "labelTemplateDescrtion",
            "labelType",
            "dptname",
            "labelColor",
            "ribbonsType",
            "printerType",
            "containSn",
            "status",
            "remark",
            "ureportId"
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);
          // debugger
          this.ureportList.forEach((ele) => {
            if ((this.model.ureportId = ele.id)) {
              this.formData.ureportName = ele.name;
              formData.ureportName = ele.name;
            }
          });
          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "createBy",
          "createTime",
          "updateBy",
          "updateTime",
          "labelTemplateNo",
          "labelTemplateName",
          "labelTemplateLength",
          "labelTemplateDescrtion",
          "labelType",
          "dptname",
          "labelColor",
          "ribbonsType",
          "printerType",
          "containSn",
          "status",
          "remark",
          "ureportId"
        )
      );
    },
  },
};
</script>